/*********************************************
 *		
 *		File Name : SceneVirtualizer.java
 *		Author    : HuangYao
 *		Email     : huangyaobyte@gmail.com
 *		Date      : Feb 14, 2014
 *		
 *********************************************/
/*
 *    
 */

package nsg2.swing;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JPanel;

import nsg2.listener.AgentModeHandler;
import nsg2.listener.AppModeHandler;
import nsg2.listener.LinkModeHandler;
import nsg2.listener.NodeModeHandler;
import nsg2.param.SwitchMode;

/**
 * @author huangyao
 *画图的控制中心类，负责加载各种Listener和各种Panel
 */
public class SceneVirtualizer extends JComponent {
	public JPanel modePanel;
	public LinkPanel linkPanel = new LinkPanel(this);
	public NodePanel nodePanel = new NodePanel(this);
	public AgentPanel agentPanel = new AgentPanel(this);
	public AppPanel appPanel = new AppPanel(this);
	
	private AgentModeHandler agentModeHandler = new AgentModeHandler();
	private AppModeHandler appModeHandler = new AppModeHandler();
	private LinkModeHandler linkModeHandler = new LinkModeHandler();
	private NodeModeHandler nodeModeHandler = new NodeModeHandler();
	
	public SceneVirtualizer() {
		super();
		switchMod(SwitchMode.NODE);
	}

	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		drawGrid(g);
		
	}

	private void drawGrid(Graphics g) {
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, 10000, 10000);
		for (int i = 100; i < 10000; i += 100) {
			g.drawLine(i, 0, i, 10000);
		}
		for (int i = 100; i < 10000; i += 100)
			g.drawLine(0, i, 10000, i);
	}

	public void switchMod(int mod) {
		if (getMouseListeners().length != 0) {
			removeMouseListener(getMouseListeners()[0]);
			removeMouseMotionListener(getMouseMotionListeners()[0]);
		}
		System.out.println(mod);
		switch (mod) {
		case 0:
			this.modePanel = nodePanel;
			this.addMouseMotionListener(nodeModeHandler);
			this.addMouseListener(nodeModeHandler);
			break;
		case 1:
			this.modePanel = linkPanel;
			this.addMouseMotionListener(linkModeHandler);
			this.addMouseListener(linkModeHandler);
			break;
		case 2:
			this.modePanel = agentPanel;
			this.addMouseMotionListener(agentModeHandler);
			this.addMouseListener(agentModeHandler);
			break;
		case 3:
			this.modePanel = appPanel;
			this.addMouseMotionListener(appModeHandler);
			this.addMouseListener(appModeHandler);
			break;
		}
		this.add(modePanel);
		this.repaint();
	}
}
